package com.itheima.dao;

import com.itheima.domain.Student;

import java.time.LocalDate;
import java.util.ArrayList;

public class ImplStudentDao implements IStudentDao {
    private static ArrayList<Student> students = new ArrayList<>();

   /* static {
        Student student0 = new Student("0001", "坤坤1", "男", "32期", 1, "13655557777", LocalDate.parse("1996-01-01"), 26);
        Student student1 = new Student("0002", "坤坤2", "男", "32期", 3, "13645557777", LocalDate.parse("1995-01-01"), 27);
        Student student2 = new Student("0003", "坤坤3", "男", "32期", 2, "13653557777", LocalDate.parse("1994-01-01"), 28);
        Student student3 = new Student("0004", "坤坤4", "男", "32期", 4, "13625557777", LocalDate.parse("1993-01-01"), 29);
        students.add(student0);
        students.add(student1);
        students.add(student2);
        students.add(student3);
    }*/

    @Override
    public int getindex(String id) {
        int index = -1;
        for (int i = 0; i < students.size(); i++) {
            Student student = students.get(i);
            if (student.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    @Override
    public void addStudent(Student stu) {
        students.add(stu);
    }

    @Override
    public ArrayList<Student> findAllStudent() {
        return students;
    }

    @Override
    public void updateStudent(String id, Student newstu) {
        int index = getindex(id);
        students.set(index, newstu);
    }

    @Override
    public void deleteStudent(String id) {
        int index = getindex(id);
        students.remove(index);
    }

    @Override
    public ArrayList<Student> findStuByName(String name) {
        ArrayList<Student> list = new ArrayList<>();
        for (Student student : students) {
            if (student.getName().equals(name)) {
                list.add(student);
            }
        }
        return list;
    }

    @Override
    public Student findStuByPhone(String phone) {
        for (Student student : students) {
            if (student.getPhone().equals(phone)) {
                return student;
            }
        }
        return null;
    }
}
